<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="项目编号" prop="project_id">
                        <el-input v-model="project_code" readonly placeholder="请选择项目" @click="checkItem"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目名称" prop="project_name">
                        <el-input v-model="project_name" readonly placeholder="请选择项目"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="项目总价" prop="total_price">
                        <el-input v-model="total_price" readonly placeholder="项目总价"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="推介人姓名" prop="recommend_name">
                        <el-input v-model="recommend_name" readonly placeholder="推介人姓名"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="提成比例" prop="commission_ratio">
                        <el-input v-model="form.commission_ratio" placeholder="提成比例" @input="count"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="提成金额" prop="commission_money">
                        <el-input v-model="form.commission_money" readonly placeholder="提成金额"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="创建时间" prop="date">
                    <el-date-picker v-model="form.create_time" type="date" placeholder="请选择创建时间" style="width: 100%;" :value-format="'YYYY-MM-DD'" disabled></el-date-picker>
                </el-form-item>
            </el-col>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <item-list v-if="dialog.item" ref="itemDialog" @success="itemBack" @closed="dialog.item=false"></item-list>
</template>

<script>
import itemList from '../list/itemList.vue';
export default {
    components: { itemList },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增推介人提成',
				edit: '编辑推介人提成',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                project_id:"",
                commission_ratio:1,
                commission_money:0,
                create_time:this.moment().format('YYYY-MM-DD'),
			},
            project_code:"",
            project_name:"",
            project_profit:"",
            total_price:"",
            recommend_name:"",
			//验证规则
			rules: {
				project_id: [{ required: true, message: '请选择项目' }],
			},
            dialog:{
                item:false,
            },
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.project.referrer.edit.post(this.form);
					} else {
						res = await this.$API.project.referrer.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        projectData(data){
            this.form.project_id = data.project_id
            this.project_code = data.project_code
            this.project_name = data.project_name
            this.recommend_name = data.recommend_name
            this.total_price = data.total_price
            this.form.commission_money = (data.total_price * Number(this.form.commission_ratio / 100)).toFixed(2)
        },
		//表单注入数据
		setData(data) {
            this.form.id = data.id
            this.form.project_id = data.project_id
            this.form.commission_ratio = data.commission_ratio
            this.form.commission_money = data.commission_money
            this.form.create_time = data.create_time
            this.project_code = data.project.project_code
            this.project_name = data.project.metting
            this.recommend_name = data.project.recommend.recommend_name
            this.project_profit = data.project.profit
            this.total_price = data.project.total_price
		},
        checkItem(){
            this.dialog.item = true
			this.$nextTick(() => {
				this.$refs.itemDialog.open('add')
			})
        },
        itemBack(data){
            this.form.project_id = data.id
            this.project_code = data.project_code
            this.project_name = data.metting
            this.recommend_name = data.recommend.recommend_name
            this.project_profit = data.profit
            this.total_price = data.total_price
            this.form.commission_money = (data.total_price * Number(this.form.commission_ratio / 100)).toFixed(2)
        },
        count(){
            if(this.form.commission_ratio){
                this.form.commission_money = (this.total_price * Number(this.form.commission_ratio / 100)).toFixed(2)
            }
        },
	}
}
</script>

<style></style>
